package edu.xhu.communityantiepidemicsystemback.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.xhu.communityantiepidemicsystemback.common.result.PageResult;
import edu.xhu.communityantiepidemicsystemback.common.result.Result;
import edu.xhu.communityantiepidemicsystemback.dto.*;
import edu.xhu.communityantiepidemicsystemback.entity.Staff;
import edu.xhu.communityantiepidemicsystemback.service.IStaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/staff")
@Slf4j
public class StaffController {

    @Autowired
    private IStaffService staffService;
    @PostMapping("/fuzzysearch")
    public PageResult<?> fuzzyStaffInfo(@RequestBody Fuzzy fuzzy){
        IPage<Staff> staffIPage= staffService.fuzzyStaffInfo(fuzzy);
        if(Objects.isNull(staffIPage)){
            return new PageResult<>();
        }
        return new PageResult<>(staffIPage.getTotal(),staffIPage.getRecords());
    }



    @PostMapping("/staffList")
    public PageResult<?> getStaffList(@RequestBody Query query){
        IPage<Staff> staffPage =staffService.selectStaffList((query));
        if (Objects.isNull(staffPage)){
            return new PageResult<>();
        }
        return new PageResult<Staff>(staffPage.getTotal(),staffPage.getRecords());
    }

    @PostMapping("/add")
    public Result<?> addStaff(@RequestBody CreatStaff creatStaff){
        boolean result=staffService.addStaff(creatStaff);
        if(result){
            return new Result<>().success();
        }else{
            return new Result<>().error("添加人员失败！");
        }
    }
    @GetMapping("/del/{id}")
    public Result<?> delStaff(@PathVariable Integer id){
        boolean result =staffService.delStaff(id);
        if (result){
            return new Result<>().success();
        }else{
            return new Result<>().error("删除失败,服务器内部错误");
        }
    }
    @PostMapping("/update")
    public Result<?> update(@RequestBody UpdateStaff updateStaff){
        boolean result =staffService.updateStaff(updateStaff);
        if (result){
            return new Result<>().success();
        }else{
            return new Result<>().error("修改失败，服务器内部错误");
        }
    }
}
